//将数据动态添加到页面上
$.ajax({
	type: "post",
	url: "http://a.91jfk.com/Product/Address/address_list",
	async: true,
	data: {},
	dataType: 'json',
	success: function(res) {
		console.log(res);
		if(res.code == '1') {
			var name;
			var tel;
			var sheng;
			var shi;
			var qu;
			var id;
			var state;
			console.log(res.msg.length);
			for(var i = 0; i < res.msg.length; i++) {
				name = res.msg[i].name;
				tel = res.msg[i].tel;
				sheng = res.msg[i].area;
				shi = res.msg[i].street;
				qu = res.msg[i].address;
				id = res.msg[i].id;
				state = res.msg[i].state;
				j = i;
				showItem(name, tel, sheng, shi, qu, id, state, j);
				//判断页面长度，改变内容底边距
				if(res.msg.length > 4) {
					$(".manage_address_content").css("padding-bottom", "1rem");
				}
				//判断设置默认地址的状态
				if(state == 1) {
					$('#lab' + i).removeClass('weixuan').addClass("yixuan");
					
				}
			}
		} 
	},
	error: function(err) {}
});

//动态创建标签
function showItem(name, tel, area, street, address, id, state, i) {
	var tr = $('<tr class="demo"></tr>');
	var td = $("<td></td>");
	var checkItem = $('<div class="check" style="display:none">' + id + '</div>');
	var div1 = $('<div class="state" style="display:none">' + state + '</div>');
	var p1 = $('<p class="one"><span class="user">' + name + '</span><span class="tel">' + tel + '</span></p>');
	var p2 = $('<p class="two"><span class="sheng">' + area + '</span><span class="shi">' + street + '</span><span class="qu">' + address + '</span></p>');
	var p3 = $('<p class="three"><span class="moren"><label for="danxuan" class="fugai weixuan" id="lab' + i + '"></label><input type="radio" id="danxuan"/><span>设为默认</span></span><span class="bianji"><span id="edit">编辑</span></span><span class="shanchu"><span id="del">删除</span></span></p>');
	td.append(p1).append(p2).append(p3).append(checkItem).append(div1);
	tr.append(td);
	$(".biao").append(tr);
}
//点击设置默认，页面不跳转
$(".biao").on("click", ".moren", function(e) {
	var id = $(this).parents(".demo").find('.check').text();
	console.log(id);
	var state = $(this).parents(".demo").find('.state').text();
	$.ajax({
		type: "post",
		url: "http://a.91jfk.com/Product/Address/default_address",
		data: {
			id: id,
		},
		datatype: "json",
		success: function(xhr) {
			console.log(xhr);
			if(xhr.code == "1") {
				window.location.href = window.location.href + "?id=" + 10000 * Math.random();
			}
		}
	})
	e.stopPropagation();
})

//编辑跳转
$(".biao").on("click", "#edit", function(e) {
	e.stopPropagation();
	var id = $(this).parents(".demo").find('.check').text();
	var state = $(this).parents(".demo").find('.state').text();
	var name = $(this).parents(".demo").children().find(".one").find(".user").text();
	var tel = $(this).parents(".demo").children().find(".one").find(".tel").text();
	var sheng = $(this).parents(".demo").children().find(".two").find(".sheng").text();
	var shi = $(this).parents(".demo").children().find(".two").find(".shi").text();
	var qu = $(this).parents(".demo").children().find(".two").find(".qu").text();
	var storage = window.localStorage;
	var obj = {
		name: name,
		id: id,
		tel: tel,
		sheng: sheng,
		shi: shi,
		qu: qu,
		state: state
	}
	var d = JSON.stringify(obj);
	console.log(d);
	storage.setItem("data", d);
	window.location.href = "jfdhbianjidizhi.html";
});

//点击任意一个tr,将信息存入缓存，跳转到确认订单页面
$(".biao").on("click", ".demo", function() {

	var id = $(this).find('.check').text();
	var state = $(this).find('.state').text();
	var name = $(this).children().find(".one").find(".user").text();
	var tel = $(this).children().find(".one").find(".tel").text();
	var sheng = $(this).children().find(".two").find(".sheng").text();
	var shi = $(this).children().find(".two").find(".shi").text();
	var qu = $(this).children().find(".two").find(".qu").text();
	var storage = window.localStorage;
	var obj = {
		name: name,
		id: id,
		tel: tel,
		sheng: sheng,
		shi: shi,
		qu: qu,
		state: state
	}
	var d = JSON.stringify(obj);
	storage.setItem("data", d);
	window.location.href = "jfdhConfirmOrder.html";
});

//跳转到添加地址页面
$(".manage_address_footer").bind("click", function() {
	window.location.href = "jfdhtianjiadizhi.html";
});
//点击删除地址
$(".biao").on("click", "#del", function(e) {
	$(".mengban").css("display", "block");
	$(".delete").css("display", "block");
	$(".yes").css("display", "block");
	e.stopPropagation();
	var id = $(this).parents(".demo").find('.check').text();
	$(".yes").bind("click", function(e) {
		e.stopPropagation();
		$.ajax({
			type: "post",
			url: "http://a.91jfk.com/Product/Address/del_address",
			data: {
				id: id
			},
			datatype: "json",
			success: function(xhr) {
				if(xhr.code == "1") {
					$(".delete").css("display", "none");
					$(".yes").css("display", "none");
					$(".mengban").css("display", "none");
					setTimeout(function() {
						window.location.href = window.location.href + "?id=" + 10000 * Math.random();
					}, 500);
				} else {
					window.location.reload();
				}
			}
		})

	});
})
//点击取消，蒙版消失，弹框消失
$(".delete p:last-child").bind("click", function() {
	$(".mengban").css("display", "none");
	$(".delete").css("display", "none");
	$(".yes").css("display", "none");
});
